#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#define MAXTMP  1024

int my_dup2(int filedes, int filedes2)
{
    int i=0, tmpfd[MAXTMP] = {0};

    if (filedes < 0 || filedes2 < 0)
        return -1;

    if (filedes == filedes2)
        return filedes;

    close(filedes2);

    while ((tmpfd[i++] = dup(filedes)) < filedes2);

    for (i = 0; tmpfd[i] != filedes2; i++) {
        close(tmpfd[i]);
    }
    
    return filedes2;
}

int main(int argc, char const* argv[])
{
    int fd = open("file", O_RDWR);

    my_dup2(fd, 1);

    printf("year\n");

    return 0;
}


